home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / Scripts / pilconvert.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  1.9 KB  |  91 lines

  1. #! /usr/local/bin/python
  2. #
  3. # The Python Imaging Library.
  4. # $Id: pilconvert.py,v 1.1.1.1 1998/08/18 13:07:59 sjoerd Exp $
  5. #
  6. # convert image files
  7. #
  8. # History:
  9. # 0.1    96-04-20 fl    Created
  10. # 0.2    96-10-04 fl    Use draft mode when converting images
  11. # 0.3    96-12-30 fl    Optimize output (PNG, JPEG)
  12. # 0.4    97-01-18 fl    Made optimize an option (PNG, JPEG)
  13. #
  14.  
  15. import Image
  16.  
  17. import getopt, string, sys
  18.  
  19. def usage():
  20.     print "PIL Convert 0.4/97-01-18 -- convert image files"
  21.     print "Usage: pilconvert [option] infile outfile"
  22.     print
  23.     print "Options:"
  24.     print
  25.     print "  -c <format>  convert to format (default is given by outfile)"
  26.     print
  27.     print "  -g           convert to greyscale"
  28.     print "  -p           convert to palette image"
  29.     print "  -r           convert to rgb"
  30.     print
  31.     print "  -o           optimize output (trade speed for size)"
  32.     print "  -q <value>   set compression quality (0-100, JPEG only)"
  33.     print
  34.     print "  -f           list supported file formats"
  35.     sys.exit(1)
  36.  
  37. if len(sys.argv) == 1:
  38.     usage()
  39.  
  40. try:
  41.     opt, argv = getopt.getopt(sys.argv[1:], "dc:gopq:r")
  42. except getopt.error, v:
  43.     print v
  44.     sys.exit(1)
  45.  
  46. format = None
  47. convert = None
  48.  
  49. options = { }
  50.  
  51. for o, a in opt:
  52.  
  53.     if o == "-f":
  54.         Image.init()
  55.     id.sort()
  56.     print "Supported formats:"
  57.     for i in id:
  58.         print i, 
  59.     sys.exit(1)
  60.  
  61.     elif o == "-c":
  62.     format = a
  63.  
  64.     if o == "-g":
  65.     convert = "L"
  66.     elif o == "-p":
  67.     convert = "P"
  68.     elif o == "-r":
  69.     convert = "RGB"
  70.  
  71.     elif o == "-o":
  72.     options["optimize"] = 1
  73.     elif o == "-q":
  74.     options["quality"] = string.atoi(a)
  75.  
  76. if len(argv) != 2:
  77.     usage()
  78.  
  79. try:
  80.     im = Image.open(argv[0])
  81.     if convert and im.mode != convert:
  82.     im.draft(convert, im.size)
  83.     im = im.convert(convert)
  84.     if format:
  85.     apply(im.save, (argv[1], format), options)
  86.     else:
  87.     apply(im.save, (argv[1],), options)
  88. except:
  89.     print "cannot convert image",
  90.     print "(%s:%s)" % (sys.exc_type, sys.exc_value)
  91.